import { BaseGraph, IBaseProps , IBoundBox} from './base'

export interface IPath extends IBaseProps {
  path: string
}

export class Path extends BaseGraph {
  protected pathString: string = ''
  constructor(props: IPath) {
    super(props)
    this.type = 'path'
    this.option.strokeStyle = this.option.fillStyle
    this.option.lineWidth = this.option.lineWidth || 1
    this.pathString = props.path
    this.setPath()
  }
  public setPath() {
    delete this.path
    this.path = new Path2D(this.pathString)
  }
  public drawPath(ctx: CanvasRenderingContext2D): void {
    if (this.isFill) ctx.fill(this.path)
    else ctx.stroke(this.path)
  }
  public clone() {
    return new Path({
      ...this.option,
      ...this.getBaseCloneAttr(),
      path: this.pathString
    })
  }
  public getBoundBox(): IBoundBox {
    let minX = Infinity
    let maxX = -Infinity
    let minY = Infinity
    let maxY = -Infinity

    return {
      top: minY + this.y,
      bottom: maxY + this.y,
      left: minX + this.x,
      right: maxX + this.x,
      width: maxX - minX,
      height: maxY - minY
    }
  }
}
